<?php
class Water1{
    #水印图片
    protected $water;
    public function __construct($water)
    {
        $this->water = $water;
    }

    #对外公开接口
    /**
     *对外公开接口
     * $desImage -> 原始图片
     * $fileName -> 文件的名称  【如果存在就用新的，如果没有就用以前的】
     * $pos -> 水印的位置   【这里使用九宫格的方式进行定位】
     */
    public function make(string $desImage,string $fileName=null,int $pos=1){
        //判断图片的异常情况
        $this->ErrorOrOkFile($desImage);
        #根据图片的类型加载图片的资源
        $image = $this->resource($desImage);
        #获取水印图片
        $water = $this->resource($this->water);
        #设置水印的位置
        $position = $this->position($image,$water,$pos);
        imagecopy($image,$water,$position['x'],$position['y'],0,0,imagesx($water),imagesy($water));
        #获取图片的输出方式
        header('Content-Type:image/png');
        $this->showAction($desImage)($image);
        #将图片进行输出操作
        $this->showAction($desImage)($image,$fileName??$desImage);
    }


    /*
     * 获取水印开始的位置
     * $image -> 原图
     * $water -> 水印图片
     * $pos -> 图片展示的位置
     * */
    protected function position($image,$water,$pos){
        $poss = ['x'=>0,'y'=>0];
        switch ($pos){
            case 1:
                break;
            case 2:
                $poss['x'] = (imagesx($image) - imagesx($water))/2;
                break;
            case 3:
                $poss['x'] = (imagesx($image) - imagesx($water));
                break;
            case 4:
                $poss['y'] = (imagesy($image) - imagesy($water)) / 2;
                break;
            case 5:
                $poss['x'] = (imagesx($image) - imagesx($water))/2;
                $poss['y'] = (imagesy($image) - imagesy($water)) / 2;
                break;
            case 6:
                $poss['x'] = (imagesx($image) - imagesx($water));
                $poss['y'] = (imagesy($image) - imagesy($water)) / 2;
                break;
            case 7:
                $poss['y'] = (imagesy($image) - imagesy($water));
                break;
            case 8:
                $poss['x'] = (imagesx($image) - imagesx($water))/2;
                $poss['y'] = (imagesy($image) - imagesy($water));
                break;
            case 9:
                $poss['x'] = (imagesx($image) - imagesx($water));
                $poss['y'] = (imagesy($image) - imagesy($water));
                break;
        }
        return $poss;
    }

    #获取图片的输出方式
    protected function showAction(string $desImage){
        $showTypeArr = [1=>'imagegif',2=>'imagejpeg',3=>'imagepng'];
        $info = getimagesize($desImage);
        return $showTypeArr[$info[2]];
    }

    #根据图片类型加载图片资源
    protected function resource(string $desImage){
        $imgCreateArr = [1=>'imagecreatefromgif',2=>'imagecreatefromjpeg',3=>'imagecreatefrompng'];
        $info = getimagesize($desImage);
        $function = $imgCreateArr[$info[2]];
        return $function($desImage);
    }

    #判断文件是不是正常
    protected function ErrorOrOkFile(string $desImage){
        if(!is_file($desImage) || !getimagesize($desImage)){
            throw new Exception('文件异常');
        }
        return true;
    }

}